<template>
  <a-modal
    :title="modalTitle"
    :visible="visible"
    centered
    width="850px"
    :maskClosable="false"
    @cancel="$emit('close')"
  >
    <div class="quik-search">
      <span>快速查询：</span>
      <slot v-bind:form="form">></slot>
      <a-input
        style="width: 320px"
        placeholder="请输入姓名,多条件以空格分开（如：张 王）"
        v-model="keywords"
        @pressEnter="searchChoose()"
      />
      <a-button type="primary" @click="searchChoose()">查询</a-button>
    </div>
    <div class="container">
      <div class="card-container">
        <span class="right-title">委员</span>
        <a-tree :default-expanded-keys="['0-0-0']">
          <a-tree-node key="0-0-0" title="parent 1-0">
            <a-tree-node key="0-0-0-0" title="leaf" />
            <a-tree-node key="0-0-0-1" title="leaf" />
            <a-tree-node key="0-0-0-2" title="leaf" />
          </a-tree-node>
          <a-tree-node key="0-0-1" title="parent 1-1">
            <a-tree-node key="0-0-1-0" title="leaf" />
          </a-tree-node>
          <a-tree-node key="0-0-2" title="parent 1-2">
            <a-tree-node key="0-0-2-0" title="leaf" />
            <a-tree-node key="0-0-2-1" title="leaf" />
          </a-tree-node>
        </a-tree>
      </div>
      <div class="right-choice">
        <!-- <span class="right-title">选择对象</span> -->
        <div class="right-title right-title-box">
          <div>
            <span>选择对象</span>
            <a-checkbox
              style="margin-left: 10px"
              :indeterminate="indeterminate"
              :checked="checkAll"
              @change="onCheckAllChange"
            >
              全选
            </a-checkbox>
          </div>
          <div>已选中（{{ this.checkedArr.length }}）</div>
        </div>
        <div class="check-list">
          <!-- 可选对象 -->

          <a-checkbox-group
            v-if="single"
            @change="checkEvent"
            v-model="checkedUser"
          >
            <a-checkbox
              v-for="item in searchUser"
              :value="item.id"
              :key="item.id"
              >{{ item.name }}</a-checkbox
            >
          </a-checkbox-group>
          <a-radio-group v-else @change="checkEvent" v-model="checkedUser">
            <a-radio
              v-for="item in searchUser"
              :value="item.id"
              :key="item.id"
              >{{ item.name }}</a-radio
            >
          </a-radio-group>
        </div>
        <span class="area-split"></span>
        <div class="checked-tags">
          <!-- 已选择对象 -->
          <a-tag
            v-for="item in checkedArr"
            :key="item.id"
            color="blue"
            closable
            @close="tagClose(item.id)"
            >{{ item.name }}</a-tag
          >
        </div>
      </div>
    </div>
    <template slot="footer">
      <div class="footer-btns">
        <a-button type="primary" @click="handleOk">确定</a-button>
        <a-button @click="reset">清空</a-button>
        <a-button @click="$emit('update:visible', false)">取消</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      // form: {
      //   query_strSex: undefined,
      //   keywords: '',
      //   strParamGroupType: '-99',
      //   strChooseType: ''
      // },
      keywords: '',
      loading: false,
      searchUser: [
        {
          id: 1,
          name: '定中四'
        },
        {
          id: 2,
          name: '定中爱'
        },
        {
          id: 3,
          name: '定试试'
        }
      ],
      checkedUser: [],
      checkedArr: [], //当前已选对象列表
      indeterminate: false,
      checkAll: false
    }
  },
  props: {
    form: {
      type: Object,
      default: () => {}
    },
    manager: {
      //默认选择 回显
      type: Array,
      default: () => []
    },
    single: {
      //是否多选
      type: Boolean,
      default: true
    },
    modalTitle: {
      //弹出层名字
      type: String,
      default: '选择机构用户'
    },
    chooseType: {
      //选择类型
      type: String,
      default: 'weiy'
    },
    visible: {
      type: Boolean,
      default: true
    }
  },
  mounted() {
    this.manager.forEach((item) => {})
  },
  methods: {
    tabSearchChoose(key) {
      switch (key) {
        case '1':
          this.searchChoose({ strSector: '' })

          break
        case '2':
          this.searchChoose({ strFaction: '' })
          break
        case '3':
          this.searchChoose({ strMemType: '' })

          break

        default:
          break
      }
    },
    //快速查询
    quikSearch() {
      if (!this.keywords) {
        this.$message.destroy()
        this.$message.warning('请输入关键字')
        return false
      }
    },
    //切换数据 渲染已勾选
    renderCheck(modelV, dataV, allV) {
      if (this.single) {
        this[modelV] = ''
        let findIndex = this.checkedArr.findIndex((x) =>
          this[dataV].some((y) => x.id === y.id)
        )
        if (findIndex > -1) {
          this[modelV] = this.checkedArr[findIndex].id
        }
      } else {
        this[modelV] = []
        this.checkedArr.forEach((x) => {
          if (this[dataV].some((y) => x.id === y.id)) {
            this[modelV].push(x.id)
          }
        })
        this[allV] = this[modelV].length === this[dataV].length ? true : false
      }
    },
    //对象列表选择
    checkEvent(e) {
      console.log(this.checkedUser)
      const tempArr = this.searchUser.filter(
        (a) => a.id === this.checkedUser || this.checkedUser.includes(a.id)
      )
      this.checkedArr = tempArr
      if (this.single) {
        this.indeterminate =
          !!this.checkedUser.length &&
          this.checkedUser.length < this.searchUser.length
        this.checkAll = this.checkedUser.length === this.searchUser.length
      }
    },
    //删除已选对象
    tagClose(id) {
      const index = this.checkedArr.findIndex(
        (item) => item === id || item.id === id
      )
      console.log(index)

      if (index > -1) {
        this.checkedArr.splice(index, 1)
        if (this.single) {
          this.checkedUser.splice(index, 1)
        } else {
          this.checkedUser = ''
        }
      }
    },
    //确定
    handleOk() {
      // dingzhongming/104512/丁忠明,dingzhijie/104619/丁质洁
      //   this.myManager = []
      //   this.checkedArr.forEach((item) => {
      //     this.myManager.push(
      //       item.strUserId + '/' + item.strId + '/' + item.strName
      //     )
      //   })
      // this.myVisible = false;
      //   this.searchData = this.checkedArr
      this.$emit('update:visible', false)
      this.$emit('handleOk', this.checkedArr)
    },
    selectChange(value) {
      var itemArr = []
      var valueArr = []
      if (value instanceof Array) {
        //多选模式下  选中的值为数组
        value.forEach((item) => {
          var obj = {}
          itemArr = item.split('/')
          obj.type = itemArr[0]
          obj.id = itemArr[1]
          obj.name = itemArr[2]
          valueArr.push(obj)
        })
      } else {
        //单选模式下  选中的值为字符串
        valueArr.push(value)
      }
      this.$emit('selectFunc', valueArr)
    },
    //清空
    reset() {
      this.checkedArr = []
      this.checkedUser = null
      this.checkAll = this.indeterminate = false
    },
    //返回勾选的对象给父组件
    getData() {
      return this.checkedArr
    },
    searchChoose(item) {
      console.log('表单:', this.form)
      let data
      this.form = Object.assign(this.form, item)
      data = Object.assign(
        {
          type: 'mo',
          tabType: 'm',
          hideSelfId: '103677',
          delIds: '',
          isThisSession: true,
          strYear: '',
          query_strJG: '',
          strSId: '',
          showmobile: false,
          strDutyTag: this.strDutyTag.join('/')
        },
        this.form
      )
      // cbdw(data).then((res) => {
      //   this.data = Object.freeze(res.data)
      // })
      // if (item) {
      //     data = Object.assign(
      //         {
      //             keywords: this.keywords,
      //             type: this.chooseType, //类型 传固定值
      //             show: "",
      //             hideSelfId: "",
      //             delIds: "",
      //             isOpenThisCppccSession: "true",
      //             showSelfWithChildId: "",
      //             strYear: "",
      //         },
      //         item
      //     );
      // } else {
      //     data = Object.assign(this.form, {
      //         keywords: this.keywords,
      //         type: this.chooseType, //类型 传固定值
      //         show: "",
      //         hideSelfId: "",
      //         delIds: "",
      //         isOpenThisCppccSession: "true",
      //         showSelfWithChildId: "",
      //         strYear: "",
      //     });
      // }
      //strSectors

      // const formData = new FormData();
      // Object.keys(data).forEach((key) => {
      //     formData.append(key, data[key]);
      // });
      // chooseSearch(formData).then((res) => {
      //     this.searchUnit = res.data.list;
      //     this.initChoose.list = res.data.list;
      // });
    },
    onCheckAllChange(e) {
      const arr = this.searchUser.map((item) => item.id)
      Object.assign(this, {
        checkedUser: e.target.checked ? arr : [],
        indeterminate: false,
        checkAll: e.target.checked,
        checkedArr: e.target.checked ? this.searchUser : []
      })
    }
  },
  computed: {
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          this.checkedArr = this.checkedArr.filter((item) => {
            return selectedRowKeys.strFactionName
          })
          let arr = []
          arr = selectedRows.filter((item) => {
            return selectedRowKeys.includes(item.strId)
          })
          this.checkedArr.push(...arr)
          this.checkedArr = Array.from(new Set(this.checkedArr))
          this.selectedRowKeys = selectedRowKeys
          this.checkedArr = this.checkedArr.filter((item) => {
            return selectedRowKeys.includes(item.strId)
          })
        },
        // onSelect: (record, selected, selectedRows, nativeEvent) => {
        //     if (selected) {
        //         this.checkedArr.push(record);
        //         this.selectedRowKeys.push(record.strId)
        //     } else {
        //         let index = this.checkedArr.findIndex(
        //             (item) => item.strId === record.strId
        //         );
        //         this.checkedArr.splice(index, 1);
        //         this.selectedRowKeys.splice(index,1)

        //     }
        // },
        // onSelectAll: (selected, selectedRows, changeRows) => {
        //     console.log(selectedRows)
        //     if(selected){
        //         // this.checkedArr.push(record);
        //         // this.selectedRowKeys.push(record.strId)
        //     }else{

        //     }
        // },
        selectedRowKeys: this.selectedRowKeys
      }
    }
  },
  watch: {}
}
</script>

<style lang="less" scoped>
.choose-object {
  .select-manager {
    width: calc(100% - 74px);
    margin-right: 10px;
  }
}

.footer-btns {
  width: 100%;
  height: 100%;
  text-align: center;
}

.quik-search {
  height: 46px;
  line-height: 32px;
  // padding-bottom: 10px;
  border-bottom: 1px solid #e8e8e8;
  margin-bottom: 10px;
  span {
    font-size: 16px;
    color: #333;
  }
  input {
    width: 200px;
    margin-right: 15px;
  }
  /deep/.ant-input {
    height: 34px;
    line-height: 34px;
    border-radius: 2px;
  }
}

.card-container {
  display: inline-block;
  background: #f5f5f5;
  border-radius: 4px;
  border: 1px solid #f5f5f5;
  width: 33%;
  // margin: 14px 0 0 0;
  overflow: hidden;
  box-shadow: 0 0 6px 0 #e2e2e2;
}
.right-title {
  display: block;
  background-color: #f5f5f5;
  height: 40px;
  line-height: 40px;
  padding-left: 16px;
}
.right-choice {
  display: inline-block;
  width: 80%;
  border-radius: 4px;
  border: 1px solid #f5f5f5;
  margin: 14px 0 0 15px;
  overflow: hidden;
  box-shadow: 0 0 6px 0 #e2e2e2;
  margin-top: 0px;

  .data-type {
    height: 260px;
    overflow-y: auto;
  }

  .list-title {
    line-height: 40px;
    height: 40px;
    padding: 0 0 0 5px;
    margin: 0 15px;
    border-bottom: 1px solid #e8e8e8;

    .check-all {
      float: right;
    }
  }

  .check-list {
    height: 310px;
    margin: 12px 15px 0 5px;

    .ant-radio-wrapper {
      margin: 0 0 10px 10px;
    }

    .ant-checkbox-wrapper {
      margin: 0 0 10px 10px;
    }
  }

  .area-split {
    display: block;
    margin: 0 15px;
    height: 1px;
    background-color: #f5f5f5;
    box-shadow: 0 0 1px 0 #e2e2e2;
  }

  .checked-tags {
    height: 70px;
    margin: 12px 15px 12px 5px;
    overflow-y: auto;
    .ant-tag {
      margin: 0 0 10px 10px;
      height: 25px;
      line-height: 25px;

      .anticon-close {
        font-size: 14px;
        color: #1890ff;

        &:hover {
          color: #1890ff;
        }
      }
    }
  }
}

.ant-select + .ant-select,
.ant-select + .ant-input {
  margin-left: 6px;
}
.container {
  .flex-space-between();
  width: 100%;
}
.card-container {
  width: 33%;
  height: 460px;
  border-radius: 4px;
  border: 1px solid #f5f5f5;
  overflow: hidden;
  box-shadow: 0 0 6px 0 #e2e2e2;
  background-color: #fff;
  > div {
    height: 100%;
    overflow-x: auto;
  }
}
.right-title-box {
  .flex-space-between();
  width: 100%;
}
</style>